package com.keyancompany.zhongbaopingtai.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.utils.DateUtils;
import com.keyancompany.zhongbaopingtai.utils.EditTextUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.DatePopupWindow;
import com.keyancompany.zhongbaopingtai.weight.MenuPopuWindow;

import java.util.Calendar;

import butterknife.Bind;
import butterknife.BindString;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 报名
 */
public class SignUpActivity extends BaseActivity {


    @BindString(R.string.sign_up_title)
    String title;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.allow_look_resume)
    RadioButton allowLook;
    @Bind(R.id.sign_up_reward)
    TextView signUpReward;
    @Bind(R.id.sign_up_content)
    EditText content;
    @Bind(R.id.date_picker_parent)
    LinearLayout pickerParent;
    @Bind(R.id.date_picker_value)
    TextView pickerValue;

    DatePopupWindow datePopupWindow;
    @Bind(R.id.niming_yes)
    RadioButton nimingYes;
    @Bind(R.id.niming_no)
    RadioButton nimingNo;
    @Bind(R.id.niming_group)
    RadioGroup nimingGroup;
    @Bind(R.id.single)
    RadioButton single;
    @Bind(R.id.organize)
    RadioButton organize;
    @Bind(R.id.role)
    RadioGroup role;
    @Bind(R.id.budget)
    EditText budget;

    private String pid;
    private int isNiMing;
    private int isTeam;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_sign_up;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        initHomeAsUpEnable(toolbar);
    }

    @Override
    protected void setUpViews() {
        pid = getIntent().getExtras().getString("projectId");

        nimingGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (radioGroup.getId() == R.id.niming_yes) {
                    isNiMing = 0;
                } else
                    isNiMing = 1;
            }
        });

        role.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (radioGroup.getId() == R.id.single) {
                    isTeam = 0;
                } else {
                    isTeam = 1;
                }
            }
        });
    }

    @OnClick(R.id.allow_look_resume)
    public void isAllowLook(View view) {
        if (allowLook.isChecked()) {
            allowLook.setChecked(false);
        } else {
            allowLook.setChecked(true);
        }
    }


    @OnClick(R.id.sign_up_reward)
    public void goToReward(View view) {
//        if (EditTextUtils.getEditTextValue(content) == null) {
//            T.showShort(mContext, R.string.please_input_sign_up_text);
//            return;
//        }
        if (EditTextUtils.getEditTextValue(budget) == null) {
            T.showShort(mContext, R.string.please_input_budget_text);
            return;
        }
        if (EditTextUtils.getTextViewValue(pickerValue) == null) {
            T.showShort(mContext, R.string.please_check_time_text);
            return;
        }
        UserAPI.bitType(pid, isNiMing, isTeam, budget.getText().toString(), pickerValue.getText().toString(), this, getLoading(R.string.watting_hint));
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = validateJson(json);
        if (base != null) {
            if (base.isSuccess()) {
                finish();
            }
        }
    }

    @OnClick(R.id.date_picker_parent)
    public void selectPickerDate(View view) {
        if (datePopupWindow == null) {
            datePopupWindow = new DatePopupWindow(mContext);
            datePopupWindow.setCliclSureListener(new MenuPopuWindow.ClickSureListener() {
                @Override
                public void clickSureLinstener(String... param) {
                    ;
                }

            });
            datePopupWindow.setDateListener(new DatePopupWindow.IOnSelectDateListener() {
                @Override
                public void selectDate(int year, int moth, int day) {
                    pickerValue.setText(year + "年" + (moth < 10 ? "0" + (moth + 1) : (moth + 1)) + "月" + (day < 10 ? "0" + day : day) + "日");
                }
            });
        }
        datePopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
